﻿using LuDragon.Common.Utils;
using LuDragon.Data.Entities;
using LuDragon.IService.System;
using LuDragon.Web.Areas.Admin.Filters;
using System;
using System.Linq;
using System.Web.Mvc;

namespace LuDragon.Web.Areas.Admin.Controllers
{
    /// <summary>
    /// 系统设置
    /// </summary>
    [CheckLogin]
    public class SettingController : BaseController
    {
        private INavigationService setService;
        private IRoleService roleService;
        public SettingController(INavigationService _setService, IRoleService _roleService)
        {
            setService = _setService;
            roleService = _roleService;
        }
        #region 视图
        /// <summary>
        /// 菜单导航列表
        /// </summary>
        /// <returns></returns>    
        public ActionResult NavigationList()
        {
            var navList = setService.GetNavigationList(AdminInfo.RoleId);
            ViewBag.Data = navList;
            return View();
        }
        /// <summary> 
        /// 菜单导航编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult NavigationEdit(int id = 0)
        {
            //获取父级菜单
            var navList = setService.GetNavigationList(AdminInfo.RoleId);
            var parentNavList = navList.Where(s => s.ParentId == 0).ToList();
            ViewBag.ParentNavList = parentNavList;
            Sys_Navigation navInfo;
            if (id > 0)
            {
                //编辑时
                navInfo = navList.Where(s => s.NavId == id).FirstOrDefault();
            }
            else
            {
                //新增时
                navInfo = null;
            }
            ViewBag.NavInfo = navInfo;
            return View();
        }
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleList()
        {
            var roleList = roleService.GetRoleList();
            ViewBag.RoleList = roleList;
            return View();
        }
        /// <summary>
        /// 角色编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleEdit(int id = 0)
        {
            var roleInfo = roleService.GetRoleList().Where(s => s.RoleId == id).FirstOrDefault();
            ViewBag.RoleInfo = roleInfo;
            return View();
        }
        /// <summary>
        /// 角色权限
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleControl()
        {
            return View();
        }
        #endregion
        #region 数据
        /// <summary>
        /// 切换启用状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangeEnabled(int navId, bool enabled)
        {
            var model = setService.Find(navId);
            model.Enabled = enabled;
            var result = setService.Update(model);
            if (enabled)
            {
                WriteLog("菜单", Common.Enums.LogMode.Enabled, model.NavName);
            }
            else
            {
                WriteLog("菜单", Common.Enums.LogMode.NotEnabled, model.NavName);
            }
            return Success(result);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(Sys_Navigation model)
        {
            var saved = setService.Save(model);
            if (model.NavId > 0)
            {
                WriteLog("菜单", Common.Enums.LogMode.Update, model.NavName);
            }
            else
            {
                WriteLog("菜单", Common.Enums.LogMode.Add, model.NavName);
            }
            return Success(saved);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Del(int id)
        {
            var model = setService.GetModel(s=>s.NavId==id);
            var del = setService.Del(model);
            WriteLog("菜单",Common.Enums.LogMode.Delete,model.NavName);
            return Success(del);
        }
        /// <summary>
        /// 保存角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveRole(Sys_Role model)
        {
            var saved = roleService.SaveRole(model);
            if (model.RoleId > 0)
            {
                WriteLog("角色", Common.Enums.LogMode.Update, model.RoleName);
            }
            else
            {
                WriteLog("角色", Common.Enums.LogMode.Add, model.RoleName);
            }
            return Success(saved);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelRole(int id)
        {
            var model = roleService.GetModel(s=>s.RoleId==id);
            var del = roleService.DelRole(id);
            WriteLog("角色",Common.Enums.LogMode.Delete,model.RoleName);
            return Success(del);
        }
        /// <summary>
        /// 菜单保存排序 id与排序字段 必须一一对应
        /// </summary>
        /// <param name="ids">以逗号分开的id字符串</param>
        /// <param name="sorts">以逗号分开的排序字符串</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveSort(string ids, string sorts)
        {
            string[] idArr = ids.TrimEnd(',').Split(',');
            string[] sortArr = sorts.TrimEnd(',').Split(',');
            var saved = setService.SaveSort(idArr, sortArr);
            if (saved)
            {
                WriteLog("菜单", Common.Enums.LogMode.Sort);
                return Success();
            }
            else {
                throw new Exception("保存排序失败！");
            }
           
        }

        #endregion
    }
}